﻿using RRQMSocket;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VisionCore;
using System.Windows.Forms;

namespace VisionCore
{
    public class Status_EQStatus : ProcedureHelper_EQ
    {
        public void Main()
        {
            if ((ToManual_BT && StepStop && !ManualMode && ((!AutoMode && !PauseMode) || DelayOnHelper_EQ.AutoMode.blnReturn)) || Kernel.Exc.DI.X050 == 0 || Kernel.Exc.DI.X058_DoorMonitor1 == 0 || Kernel.Exc.DI.X059_DoorMonitor2 == 0)
            {
                ManualMode = true;
            }
            ToManual_BT = false;

            if (ToAuto_BT && ManualMode && Kernel.Exc.DI.X050 == 1 && (Kernel.Exc.DI.X058_DoorMonitor1 == 1&& Kernel.Exc.DI.X059_DoorMonitor2 == 1))
            {
                ManualMode = false;
            }
            ToAuto_BT = false;
          
            
            if (ToDryRun_BT && ManualMode)
            {
                if (DryRunMode)
                {
                    DryRunMode = false;
                    ParameterHelper.CV1 = new ChipInfo();
                    ParameterHelper.CV2 = new ChipInfo();
                    ParameterHelper.Insp1 = new ChipInfo();
                    ParameterHelper.Insp2 = new ChipInfo();
                    ParameterHelper.Insp3 = new ChipInfo();
                    ParameterHelper.Insp4 = new ChipInfo();
                    ParameterHelper.ULDTR2 = new DieInfo();

                    Insp1_Pass = false;
                    Insp2_Pass = false;
                    Insp3_Pass = false;
                    Insp4_Pass = false;

                    ProcedureHelper_Exc.CodeRead1_Pass = false;
                    ProcedureHelper_Exc.CodeRead2_Pass = false;

                }
                else if(EQ_Empty)
                {
                    if (!ProcedureHelper_EQ.EQ_Empty)
                    {
                        MessageBox.Show("请清空机台在席，再切换空运转！");
                        return;
                    }
                    DryRunMode = true;
                    Insp1_Pass = true;
                    Insp2_Pass = true;
                    Insp3_Pass = true;
                    Insp4_Pass = true;

                    ProcedureHelper_Exc.CodeRead1_Pass = true;
                    ProcedureHelper_Exc.CodeRead2_Pass = true;
                }                
            }
            ToDryRun_BT = false;
            AllAxisStop = Kernel.motion.CV1_TrackSW_Y.AxisMotionStatus.Stop == 1 &&
                          Kernel.motion.CV1_Y.AxisMotionStatus.Stop == 1 &&
                          Kernel.motion.CV2_TrackSW_Y.AxisMotionStatus.Stop == 1 &&
                          Kernel.motion.CV2_Y.AxisMotionStatus.Stop == 1 &&
                          Kernel.motion.CCD1_X.AxisMotionStatus.Stop == 1 &&
                          Kernel.motion.CCD1_Y.AxisMotionStatus.Stop == 1 &&
                          Kernel.motion.CCD1_Z.AxisMotionStatus.Stop == 1 &&
                          Kernel.motion.CCD2_X.AxisMotionStatus.Stop == 1 &&
                          Kernel.motion.CCD2_Y.AxisMotionStatus.Stop == 1 &&
                          Kernel.motion.CCD2_Z.AxisMotionStatus.Stop == 1 &&
                          Kernel.motion.Insp1_Y.AxisMotionStatus.Stop == 1 &&
                          Kernel.motion.Insp2_Y.AxisMotionStatus.Stop == 1 &&
                          Kernel.motion.Insp3_Y.AxisMotionStatus.Stop == 1 &&
                          Kernel.motion.Insp4_Y.AxisMotionStatus.Stop == 1;

            ToAutoRun = ToAutoRun_BT && Door_Lock && !ManualMode && HomeComp && (PauseMode || StepStop);
            ToAutoRun_BT = false;

            ToInit = ToInit_BT && !ManualMode && !AutoMode && !PauseMode && Kernel.Exc.DI.X050 == 1 && (Kernel.Exc.DI.X058_DoorMonitor1 == 1&& Kernel.Exc.DI.X059_DoorMonitor2 == 1);
            ToInit_BT = false;

            //ToForceStop = !ManualMode && ToStop_BT && (InitMode || PauseMode || ((StepStop || AutoMode) && !ProcedureHelper_Exc.ExcTR_Exchanging && !ProcedureHelper_Exc.ULDSTFocusTrayChange));
            ToForceStop = !ManualMode && AllAxisStop && StepStop  && !ProcedureHelper_Exc.ExcTR_Exchanging && !ProcedureHelper_Exc.ULDSTFocusTrayChange && ToStop_BT_ForceStop ;
            if (/*ToForceStop*/DelayOnHelper_EQ.ToForceStop.blnReturn)
            {
                InitMode = false;
                PauseMode = false;
                AutoMode = false;
                ManualMode = true;
                //ToStop_BT_ForceStop = false;
            }

            InitMode = ToInit || (InitMode && !StepStop && !HomeComp);

            AutoMode = ToAutoRun || (AutoMode && !StepStop && !ManualMode);

            StepStop = ToStop_BT || (!ManualMode && !InitMode && !AutoMode && !PauseMode) || (StepStop && !ManualMode && !InitMode && !AutoMode && !DelayOnHelper_EQ.ToForceStop.blnReturn);
           
            ToStop_BT=false;

            if (AutoMode)
            {
                RTCHelper.DIO.L_MotionStatus = 1;
            }
            else if (ManualMode)
            {
                RTCHelper.DIO.L_MotionStatus = 2;
                ToStop_BT_ForceStop = false;
            }

            DelayOnHelper_EQ.AutoMode.Trigger = AutoMode;
            DelayOnHelper_EQ.ToInit.Trigger = ToInit;
            DelayOnHelper_EQ.ToForceStop.Trigger = ToForceStop;
            DelayOnHelper_EQ.AllAxisStop.Trigger = AllAxisStop;

            HomeComp = ExcHomeComp;                       
            
            if (!RTCHelper.IOEvent.Alive_RemoteExist.AliveBeatTimeOut && RTCHelper.DIO.R_VisionPC_RunMode==1)
            {
                VisionAutoMode = true;
            }
            else
            {
                VisionAutoMode = false;
            }
        }
    }
}
